package com.httpClient.chapter2;

import com.JavaNet.chapter8_client_socket.SocketForHttp;
import com.httpClient.util.ResponseUtil;
import com.sky.JULLog.MyJulLogger;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MIME;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHeaderElementIterator;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

/**
 * 对比 {@link  SocketForHttp }  ,
 *
 * @author : wushikai
 *         date : 2024-06-17
 */
public class JsonFormHttpClient {


    private static Logger log = MyJulLogger.getLogger(JsonFormHttpClient.class);


    public static void main(String[] args) throws Exception {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");   //这样会输出代理class文件
        doRequest();
    }


    private static void doRequest( ) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();

        /*-----------------   mark 组装一个 post  json  请求体   -------------*/
        String jsonStr = "{\"acctBranchBank\":\"中国建设银行股份有限公司罗城支行\",\"legalName\":\"韦周林\",\"acctNo\":\"6236683430000264512\",\"province\":\"广西\",\"id\":55911387,\"acctBank\":\"105628319296\",\"acctType\":\"私户\",\"finContactTel\":\"18677833308\",\"merchantNo\":\"5536256232545S9\",\"syncTime\":\"2021-12-28\",\"city\":\"河池\",\"merShortname\":\"广西罗城老兵供应链管理有限公司\",\"legalIdcardValidDate\":\"2038-01-16\",\"legalIdcard\":\"452723198601102454\",\"columnDefaultMap\":{},\"merchantName\":\"广西罗城老兵供应链管理有限公司\",\"finContact\":\"韦周林\",\"changeDate\":\"2021-12-28\",\"belongBranch\":\"柳州\",\"folderDate\":\"2021-12-28\",\"expander\":\"韦丰盛\",\"synchronizer\":\"suruiyan\",\"licenseValidDate\":\"2099-12-31\",\"maintainBranch\":\"柳州\",\"regDate\":\"2021-11-22\",\"regAddress\":\"广西河池市罗城仫佬族自治县东门镇城南二环路北面金狮城D地块59栋1层3号车库\",\"merStatus\":\"正常\",\"mcc42\":\"4225\",\"acctName\":\"韦周林\"}";


        StringEntity jsonEntity = new StringEntity(jsonStr, StandardCharsets.UTF_8);
//        Header content_type = new BasicHeader( MIME.CONTENT_TYPE, "application/x-www-form-urlencoded");
        Header content_type = new BasicHeader(MIME.CONTENT_TYPE, "application/json");  //mark 头不能错,  否则 服务器将不会读取 post 内容 ;
        jsonEntity.setContentType(content_type);
        jsonEntity.setContentEncoding(StandardCharsets.UTF_8.name());
        /*-----------------                                  -------------*/

        HttpPost httpPost = new HttpPost("http://localhost:8080/gx/test/test2");
        httpPost.setEntity(jsonEntity);
        HttpResponse response = httpclient.execute(httpPost);  //使用 httpClient 调用 get请求
        ResponseUtil.handleResponse(response);

    }


}
